/*
 * Copyright (c) 2009, Hamish Morgan. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the University of Sussex nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package locusts.server.physics;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import locusts.common.entities.CropsEntity;
import locusts.common.entities.Entity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A rudementary system of joints that enable one entity to become attached to
 * another. Used primerally for user input.
 *
 * @author Hamish Morgan
 */
public class PhysicsSystem {

    private static final Logger LOG = LoggerFactory.getLogger(
            PhysicsSystem.class);
    private final List<Joint> joints;

    public PhysicsSystem() {
        joints = new CopyOnWriteArrayList<Joint>();
    }

    // replaces if the new join is higher z
    public void addZJoint(Joint j) {
        List<Joint> existing = getJointsContaining(j.getEntityA());
        existing.addAll(getJointsContaining(j.getEntityB()));
        boolean foundbetter = false;

        for (Joint e : existing) {
//            System.out.println(e.getEntityA().getTypeId());
            if (e.getEntityA().getzOrder() > j.getEntityA().getzOrder() || (e.
                    getEntityA().getzOrder() == j.getEntityA().getzOrder() && e.getEntityA().
                    getId() < j.getEntityA().getId()) //                    || e.getEntityB().getzOrder() > j.getEntityB().getzOrder()
                    //                    || e.getEntityA().getId() > j.getEntityA().getId()
                    //                    || e.getEntityB().getId() > j.getEntityB().getId()
                    ) {
                foundbetter = true;
            }
        }

        if (!foundbetter) {
            removeJointsContaining(j.getEntityA());
            removeJointsContaining(j.getEntityB());

            if (j.getEntityA() instanceof CropsEntity) 
                ((CropsEntity) j.getEntityA()).setPickup(true);
            
            j.getEntityA().setModified();
            j.getEntityB().setModified();
            joints.add(j);
        }
    }

    public void addJoint(Joint j) {

        if (j.getEntityA() instanceof CropsEntity) {
            ((CropsEntity) j.getEntityA()).setPickup(true);
        }
        j.getEntityA().setModified();
        j.getEntityB().setModified();
        joints.add(j);
    }

    public void removeJoint(Joint j) {
        joints.remove(j);
    }

    public List<Joint> getJointsContaining(Entity e) {
        List<Joint> result = new ArrayList<Joint>();
        for (Joint j : joints) {
            if (j.getEntityA().equals(e) || j.getEntityB().equals(e)) {
                result.add(j);
            }
        }
        return result;
    }

    public void removeJointsContaining(Entity e) {
        if (joints.size() == 0)
            return;

        List<Joint> removals = new ArrayList<Joint>();

        for (Joint j : joints) {
            if (j.getEntityA().equals(e) || j.getEntityB().equals(e)) {
                removals.add(j);
            }
        }

        for (Joint r : removals) {
            if (r.getEntityA() instanceof CropsEntity) {
                ((CropsEntity) r.getEntityA()).setPickup(false);
                r.getEntityA().setModified();
            }
            if (r.getEntityB() instanceof CropsEntity) {
                ((CropsEntity) r.getEntityB()).setPickup(false);
                r.getEntityB().setModified();
            }
        }

        joints.removeAll(removals);
    }

    public void update(double period) {
        if (joints.size() == 0)
            return;
        for (Joint j : joints) {
            j.update(period);
        }
    }
}
